import fetchJsonp from "fetch-jsonp"

let url="https://mce.mogucdn.com/jsonp/multiget/3"
let guessUrl="https://list.mogujie.com/search"
let classifyLeftUrl="https://mce.mogucdn.com/jsonp/multiget/3"
let rightTopUrl="https://mce.mogujie.com/jsonp/makeup/3"
let rightDownUrl="https://list.mogujie.com/search"
let topSearch="https://list.mogujie.com/module/mget"
let goodsShowUrl="https://list.mogujie.com/search"
let goodsCommentList="https://rate.mogujie.com/h5/rate/jsonp/x6.rate.ratelist/v1"
function dataFormat(data){
    let dataString="";
    for(let key in data){
        dataString+=`${key}=${data[key]}&`
    }
    return dataString.substring(0,dataString.length-1);
}
export default {
    loadData(data){
        return fetchJsonp(url+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },

    loadGuessData(data){
        return fetchJsonp(guessUrl+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },

    loadClassifyLeftData(data){
        return fetchJsonp(classifyLeftUrl+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },

    loadClassifyRightTopData(data){
        return fetchJsonp(rightTopUrl+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })

    },
    loadClassifyRightDownData(data){
        return fetchJsonp(rightDownUrl+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },
    //搜索框数据
    loadTopSearchData(data){
        return fetchJsonp(topSearch+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },

    loadGoodsShowData(data){
        return fetchJsonp(goodsShowUrl+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },

    loadGoodsCommentList(data){
        return fetchJsonp(goodsCommentList+"?"+dataFormat(data))
            .then(response=>{
                if (response.ok) {
                    return response.json();
                } else {
                    return {
                        success: false,
                        data: {
                            msg: "请求失败"
                        }
                    };
                }
            })
    },
}